import Header from '../../../components/Header.vue'
import persons from "../../../pages/persons";
import mediaVue from "../../media";
import device from "../../../pages/device";
import deviceAll from "../../../pages/device/all";
import videoConference from "../../../pages/videoConference";
import TreeGrid from '../../../components/TreeGrid.vue';

export default {
    name: 'company',
    components: {
        Header,
        persons,
        mediaVue,
        device,
        deviceAll,
        videoConference,
        TreeGrid
    },
    data() {
        return {
            columns: [
                {
                    label: this.$t('register.companyName'),
                    dataIndex: 'text'
                },
                {
                    label: this.$t('company.resolvingPower'),
                    dataIndex: 'resolution'
                }
            ],
            dataSource: [],
            loading: true,
            activeName: 'person',
            data4: 'hello',
            name: 'shengwm',
            flag: false,
            treeData: [],
            showdialog: false,
            saving: false,
            dataImageList: '',
            addForm: {
                id: null,
                companyName: '',
                contact: '',
                phone: '',
                email: '',
                address: '',
                resolution: '',
                totalDuration: ''
            },
            //存放分页数据
            pageInfo: {
                currentPage: 1,
                pageSize: 10,
                totalNum: 0
            },
            addRules: {
                title: [
                    {required: true, message: this.$t('message.required'), trigger: 'blur'},
                    {max: 20, message: this.$t('message.length20'), trigger: 'blur'},
                    {pattern: /^([\u4e00-\u9FFFa-zA-Z0-9+\s]+)$/, message: this.$t('message.incorrectFormat'), trigger: 'blur'}
                ],
                contact: [
                    {required: true, message: this.$t('message.required'), trigger: 'blur'},
                    {max: 20, message: this.$t('message.length20'), trigger: 'blur'},
                ],
                phoneno: [
                    {required: true, message: this.$t('message.required'), trigger: 'blur'},
                    {max: 20, message: this.$t('message.length20'), trigger: 'blur'},
                    {pattern: /(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14}/, message: this.$t('message.incorrectFormat'), trigger: 'blur'}
                ],
                email: [
                    {required: true, message: this.$t('message.required'), trigger: 'blur'},
                    {max: 50, message: this.$t('message.length50'), trigger: 'blur'},
                    {
                        type: 'email',
                        message: this.$t('message.mailFormat'),
                        trigger: 'blur'
                    }
                ],
                resolution: [
                    {required: true, message: this.$t('message.required'), trigger: 'blur'},
                ],
                totalDuration: [
                    {required: true, message: this.$t('message.required'), trigger: 'blur'},
                    {max: 20, message: this.$t('message.length20'), trigger: 'blur'},
                    {pattern: /^[0-9]+([.]{1}[0-9]+){0,1}$/, message: this.$t('message.incorrectFormat'), trigger: 'blur'}
                ]
            },
            face: {
                name: '',
                group: '',
                file: ''
            },
            defaultProps: {
                children: 'children',
                label: 'text',
                id: 'id'
            },
            groupName: '',
            company_name_query: '',
            showdialogroot: false,
            addFormVisible: false,
            formTitle: this.$t('user.newUser'),
            curpid: 0,
            showMain: false,
            groupid: 0,
            titleForm: '',
            //resolutionList: [],
            user: {},
            resolutionTypes: [],
            node: {}
        }
    },
    mounted() {
        this.user = JSON.parse(localStorage.getItem('userInfo'));
        this.queryPage(1);
    },
    methods: {
        showGroupList() {
            this.axios.hPost("/api/company", {}, (data) => {
                if (data.data) {
                    this.treedata = data.data;
                    this.shownode(data.data[0]);
                }
            });
        },
        //查询
        queryPage: function (pageNum) {
            this.loading = true;
            let params = {
                companyName: this.company_name_query
            };
            let url = '/api/company?pageIndex=' + pageNum + '&pageSize=' + this.pageInfo.pageSize;
            this.axios.hPost(url, params, (response) => {
                if (!response.success) {
                    this.$message.error(response.msg);
                }
                if (response.data.list) {
                    this.dataSource = response.data.list;
                    this.pageInfo.totalNum = response.data.totalCount;
                } else {
                    this.dataSource = response.data;
                    this.pageInfo.totalNum = 1;
                }
                this.pageInfo.currentPage = pageNum;
                this.loading = false;
            });
        },
        //显示增加窗口()
        showAddDialog(){
            this.formTitle = this.$t('company.newCompany');
            if (this.$refs['addForm']) {
                this.$refs['addForm'].resetFields();
                this.addForm = Object.assign(this.addForm, this.defaultData);
            }
            this.$router.push({
                name: 'companyadd',
                params: {
                    parentId: '0',
                    add_sub: true
                }
            });
        },
        //保存或修改单位
        saveOrUpdateUser(){
            let url = '/api/company/insert';
            if (!!this.addForm.id) {
                url = '/api/company/update';
            }
            this.$refs['addForm'].validate((valid) => {
                if (valid) {
                    let params = {
                        id: this.addForm.id,
                        companyId: this.addForm.company_id,
                        websiteName: this.addForm.website_name,
                        mediaType: this.addForm.media_type,
                        mediaName: this.addForm.media_name,
                        mediaEntrance: this.addForm.media_entrance,
                        oldMediaEntrance: this.addForm.old_media_entrance,
                        entranceMd5: this.$getMD5(this.addForm.media_entrance),
                        crawlCategoryName: this.addForm.crawl_category_name,
                        crawlCategoryUrl: this.addForm.crawl_category_url,
                        serviceIdList: this.addForm.service_id_list
                    };
                    this.axios.hPost(url, params, (response) => {
                        if (response.success) {
                            this.$message.success(response.msg);
                            this.addFormVisible = false;
                            this.query(1);
                        } else {
                            this.$message.error(response.msg);
                        }
                        this.saving = true;
                        setTimeout(() => {
                            this.$refs.tree.setCheckedKeys([]);
                        }, 100);
                    });
                } else {
                    this.saving = true;
                    return false;
                }
            });
        },
        tabClick(tab) {
            if (tab.name === 'media') {
                this.$refs.media.getCompanyId();
                this.$refs.media.query(1);
            } else if (tab.name === 'person') {
                this.$refs.persons.getCompanyId();
                this.$refs.persons.query(1);
            } else if (tab.name === 'device') {
                this.$refs.device.getCompanyId();
                this.$refs.device.query(1);
            } else if (tab.name === 'videoConference') {
                this.$refs.videoConference.getCompanyId();
                this.$refs.videoConference.query(1);
            }
        },
        //分页size变化
        handleSizeChange(val) {
            this.pageInfo.pageSize = val;
            this.queryPage(1);
        },
        //翻页
        handleCurrentChange(val) {
            this.queryPage(parseInt(val));
        },
        addNode(node, data) {

            if (node.level >= 5) {
                this.$message.error(this.$t('message.bottom'));
                return;
            }

            this.titleForm = this.$('company.newGroup');
            this.curpid = data.id;
            this.showdialogroot = true;
            this.flag = true;
            if (this.$refs['addForm']) {
                this.$refs['addForm'].resetFields();
            }
            this.addForm.id = null;
            this.addForm.title = '';
            this.addForm.contact = '';
            this.addForm.phoneno = '';
            this.addForm.email = '';
            this.addForm.resolution = '';
            this.addForm.totalDuration = '';

            var resolutionTypeList = this.$getDictList("device_resolution");
            var resolutionTypes1 = [];
            resolutionTypeList.forEach(function (e) {
                if (parseInt(e.value) <= parseInt(data.data.resolution)) {
                    resolutionTypes1.push(e);
                }
            });
            this.resolutionTypes = resolutionTypes1;
        },
        delNode(node, data) {
            this.$confirm(this.$t('message.DDGroup'), this.$t('standard.prompt'), {
                confirmButtonText: this.$t('standard.confirm'),
                cancelButtonText: this.$t('standard.cancel'),
                type: 'warning'
            }).then(() => {
                this.axios.hPost('/api/company/delete', {id: data.id}, (response) => {
                    this.showMain = false;
                    if (response.success) {
                        const parent = node.parent;
                        const children = parent.data.children || parent.data;
                        const index = children.findIndex(d => d.id === data.id);
                        children.splice(index, 1);
                        this.$message.success(response.msg);
                        this.showGroupList();
                    } else {
                        this.$message.error(response.msg);
                    }
                });
            });
        },
        editNode(node, data) {
            this.titleForm = this.$t('company.modifyGroup');
            this.showdialogroot = true;
            this.addForm.id = data.id;
            this.addForm.resolution = data.data.resolution;
            this.addForm.totalDuration = data.data.totalDuration + "";
            this.flag = false;
            this.node = node;

            var resolutionTypeList = this.$getDictList("device_resolution");
            var resolutionTypes1 = [];
            if (!node.parent.parent) {
                this.resolutionTypes = resolutionTypeList;
            } else {
                resolutionTypeList.forEach(function (e) {
                    if (parseInt(e.value) <= parseInt(node.parent.data.data.resolution)) {
                        resolutionTypes1.push(e);
                    }
                });
                this.resolutionTypes = resolutionTypes1;
            }

        },
        shownode(node) {
            this.groupName = node.text;
            this.showMain = true;
            this.groupid = node.id;
            this.activeName = "device";
            localStorage.setItem("companyId", node.id);
            localStorage.setItem("company", JSON.stringify(node.data));

            this.$refs.device.getCompanyId();
            this.$refs.device.query(1);
        },
        handleAddTop() {
            this.titleForm = this.$t('company.newGroup');
            this.showdialogroot = true;
            this.flag = true;
            if (this.$refs['addForm']) {
                this.$refs['addForm'].resetFields();
            }
            this.addForm.id = null;
            this.addForm.title = '';
            this.addForm.contact = '';
            this.addForm.phoneno = '';
            this.addForm.email = '';
            this.addForm.resolution = '';
            this.addForm.totalDuration = '';
            this.curpid = 0;
            this.resolutionTypes = this.$getDictList("device_resolution");
        },
        addGroup() {
            if (!!this.node.childNodes) {
                if (!!this.addForm.id) {
                    var flag = false;
                    this.node.childNodes.find(item => {
                        if (item.data.data.resolution > this.addForm.resolution) {
                            this.$message.error(this.$t('message.resolvingPower'));
                            flag = true;
                            return;
                        }
                    });
                    if (flag) {
                        return;
                    }
                }
            }
            let url = '/api/company/insert';
            if (!!this.addForm.id) {
                url = '/api/company/update';
            }
            this.saving = true;
            this.$refs['addForm'].validate((valid) => {
                if (valid) {
                    let params = {
                        id: this.addForm.id,
                        companyName: this.addForm.title,
                        companyExtraInfo: {
                            contact: this.addForm.contact,
                            phoneno: this.addForm.phoneno,
                            email: this.addForm.email
                        },
                        resolution: this.addForm.resolution,
                        totalDuration: this.addForm.totalDuration,
                        parentId: this.curpid
                    };
                    this.axios.hPost(url, params, (response) => {
                        if (response.success) {
                            this.$message.success(response.msg);
                            this.showdialogroot = false;
                            this.showGroupList();
                        } else {
                            this.$message.error(response.msg);
                        }
                        this.saving = false;
                    });
                } else {
                    this.saving = false;
                    return false;
                }
            });
        },
        addSubcompany()
        {

        },
        deleteCompany()
        {

        },
        detailInfo()
        {

        }
    },
    watch: {
        showMain (val) {
            if (!val) {
                this.$refs.deviceAll.findCompanyList();
            }
        }
    }
}